///////
//
//  Table manipulation thanks to: http://net.tutsplus.com/tutorials/javascript-ajax/using-jquery-to-manipulate-and-filter-data/
//
//////


//
//  Toggle form element visibility where required
function checkReaction() {
	if($('#reaction').val() == "prodtest") {
		$('td[name="tdleft"]').hide();  
		$('td[name="tdright"]').hide();  
		$('td[name="tdsite"]').hide();  
		$('td[name="tdscope"]').hide();  
		$('td[name="tdshow"]').hide();
		$('td[name="tdempties"]').hide();  
		$('td[name="tdtool"]').hide();  
  	
  	} else if($('#reaction').val() == "compare") {
		$('td[name="tdleft"]').show();  
		$('td[name="tdright"]').show();  
		$('td[name="tdsite"]').hide();  
		$('td[name="tdscope"]').show();  
		$('td[name="tdshow"]').show();  
		$('td[name="tdempties"]').hide();  
		$('td[name="tdtool"]').hide();  

  	} else if($('#reaction').val() == "comparekeyroles") {
		$('td[name="tdleft"]').hide();  
		$('td[name="tdright"]').hide();  
		$('td[name="tdsite"]').show();  
		$('td[name="tdscope"]').show();  
		$('td[name="tdshow"]').show();  
		$('td[name="tdempties"]').hide();  
		$('td[name="tdtool"]').hide();  

  	} else if($('#reaction').val() == "proddev") {
		$('td[name="tdleft"]').hide();  
		$('td[name="tdright"]').hide();  
		$('td[name="tdsite"]').hide();  
		$('td[name="tdscope"]').hide();  
		$('td[name="tdshow"]').hide();  
		$('td[name="tdempties"]').hide();  
		$('td[name="tdtool"]').hide();  
		
	} else if($('#reaction').val() == "nocompare") {
		$('td[name="tdleft"]').hide();  
		$('td[name="tdright"]').hide();  
		$('td[name="tdsite"]').show();  
		$('td[name="tdscope"]').hide();  
		$('td[name="tdshow"]').hide();  
		$('td[name="tdempties"]').hide();  
		$('td[name="tdtool"]').hide();  	

	} else if($('#reaction').val() == "toolcompare") {
		$('td[name="tdleft"]').hide();  
		$('td[name="tdright"]').hide();  
		$('td[name="tdsite"]').show();  
		$('td[name="tdscope"]').hide();  
		$('td[name="tdshow"]').hide();  
		$('td[name="tdempties"]').hide();  
		$('td[name="tdtool"]').show();  

	} else if($('#reaction').val() == "alltools") {
		$('td[name="tdleft"]').hide();  
		$('td[name="tdright"]').hide();  
		$('td[name="tdsite"]').show();  
		$('td[name="tdscope"]').hide();  
		$('td[name="tdshow"]').hide();  
		$('td[name="tdempties"]').hide();  
		$('td[name="tdtool"]').hide();  

	} else {
		$('td[name="tdleft"]').hide();  
		$('td[name="tdright"]').hide();  
		$('td[name="tdsite"]').show();  
		$('td[name="tdscope"]').show();  
		$('td[name="tdshow"]').show();  
		$('td[name="tdempties"]').show();  
		$('td[name="tdtool"]').hide();  
		
	}
}

function toggleEmptyTables() {
	if($('select[name="empties"]').val() == "show") {
		$(".emptyTable").show().removeClass("actions");		
	} else {
		$(".emptyTable").hide().addClass("actions");
	}
}

function cancelPage() {
	window.content.location.href = 'roles.php';
}

function popup(node, system, roleId) {
	var	URL = "https://learn"+ system +".uwaterloo.ca/d2l/lp/security/export_file.d2l?ou=6606&roleId="+ roleId;
	ifrm = document.createElement("IFRAME");
   	ifrm.setAttribute("id", "ifrm" + node);
	ifrm.setAttribute("name", "ifrm" + node);
   	ifrm.style.width = 900+"px";
   	ifrm.style.height = 300+"px";

	$('#ifrm'.node).load(function() {
		alert(this.id);
        callback(this);
    });
   	ifrm.setAttribute("src", URL);

	$('a[name="'+node+'"]').html("");

   	document.getElementsByName(node).item(0).appendChild(ifrm);

}


function getProdRoles() {
	$(".prodRole").click();
}

function getTestRoles() {
	$(".testRole").click();	
}

function getDevRoles() {
	$(".devRole").click();	
}

//filter results based on query  
function filter(selector, query) {  
  query =   $.trim(query); //trim white space  
  query = query.replace(/ /gi, '|'); //add OR for regex query  

  $(selector).each(function() {  
    ($(this).text().search(new RegExp(query, "i")) < 0) ? $(this).hide().removeClass('visible').addClass("actions") : $(this).show().addClass('visible').removeClass("actions");  
	});  
}
